/**
 * 
 */
package no.hio.wsprovider.dao;

import java.util.NoSuchElementException;

import junit.framework.TestCase;
import no.hio.wsprovider.vo.StockItem;

import org.junit.Before;
import org.junit.Test;

/**
 * @author Henning
 *
 */
public class StockItemDAOSimpleImplTestCase extends TestCase
{
	StockItem stockItem1 = new StockItem();
	StockItem stockItem2 = new StockItem();
	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{
		stockItem1.setProductID(3);
		stockItem1.setNumInStock(21);
		stockItem1.setStockLocation("21h5");
		stockItem2= new StockItem();
		stockItem2.setProductID(4);
		stockItem2.setNumInStock(22);
		stockItem2.setStockLocation("22g2");	
	}

	/**
	 * Test method for {@link no.hio.wsprovider.dao.StockItemDAOSimpleImpl#findAll()}.
	 */
	@Test
	public void testFindAll()
	{
		StockItemDAO dao = new StockItemDAOSimpleImpl();
		dao.insert(stockItem1);
		assertEquals(3, dao.findAll().size());
	}

	/**
	 * Test method for {@link no.hio.wsprovider.dao.StockItemDAOSimpleImpl#findByProductID(int)}.
	 */
	@Test
	public void testFindByProductID()
	{
		StockItemDAO dao = new StockItemDAOSimpleImpl();
		assertEquals("23g2", dao.findByProductID(2).getStockLocation());
	}

	/**
	 * Test method for {@link no.hio.wsprovider.dao.StockItemDAOSimpleImpl#insert(no.hio.wsprovider.vo.StockItem)}.
	 */
	@Test
	public void testInsert()
	{
		StockItemDAO dao = new StockItemDAOSimpleImpl();
		int b = dao.findAll().size();
		dao.insert(stockItem1);
		dao.insert(stockItem2);
		int c = dao.findAll().size();
		assertEquals(2, c-b);
	}

	/**
	 * Test method for {@link no.hio.wsprovider.dao.StockItemDAOSimpleImpl#remove(no.hio.wsprovider.vo.StockItem)}.
	 */
	@Test
	public void testRemove()
	{
		StockItemDAO dao = new StockItemDAOSimpleImpl();
		dao.insert(stockItem1);
		dao.insert(stockItem2);
		dao.remove(stockItem1);
		assertEquals(3, dao.findAll().size());
	}

	/**
	 * Test method for {@link no.hio.wsprovider.dao.StockItemDAOSimpleImpl#update(no.hio.wsprovider.vo.StockItem)}.
	 */
	@Test
	public void testUpdateSucess()
	{
		StockItemDAO dao = new StockItemDAOSimpleImpl();
		
		dao.insert(stockItem1);
		stockItem1.setStockLocation("stedet");
		dao.update(stockItem1);
		assertEquals("stedet", dao.findByProductID(3).getStockLocation());
		
	}
	
	/**
	 * Test method for {@link no.hio.wsprovider.dao.StockItemDAOSimpleImpl#update(no.hio.wsprovider.vo.StockItem)}.
	 */
	@Test
	public void testUpdateFail()
	{
		StockItemDAO dao = new StockItemDAOSimpleImpl();
		
		dao.insert(stockItem1);
		try
		{
			//test exc when update fail
			stockItem1.setProductID(222);
			dao.update(stockItem1);
			fail("insert ga ikke exception med feil id nr");
		}
		catch(NoSuchElementException nse)
		{	
		}
		
	}


}
